<?php

class HandleEvent
{
    protected $eventBase;
    protected $client;
    protected $whats;

    public function __construct(\EventBase $eventBase, $client, $whats)
    {
        $this->eventBase = $eventBase;
        $this->client = $client;
        $this->whats = $whats;
    }

    public function handle(&$events)
    {
        $event = new \Event(
            $this->eventBase,
            $this->client,
            $this->whats,
            function ($conn) use (&$events) {
                $data = fread($conn, 1024);

                print_r($data . PHP_EOL);

                fwrite($conn, 'Hello 你好' . PHP_EOL);

                fclose($conn);

                $events[(int) $conn][$this->whats]->free();
            });

        $event->add();

        $events[(int) $this->client][$this->whats] = $event;

        print_r($events);
    }

}